package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class OPrint {
	
	public String Data = "";   //文档中读出的元数据
	public String xmlData = "";  //程序处理成 xml 格式数据
	public String htmlData = "";   //程序处理成 html 格式数据
	public String path;           //输出文件的路径
	public String sp;             //系统分割线，windows，linux ：\  , /
	
	public OPrint(){
		this.sp = File.separator;
	}
	
	public void printHtml(String Data,String path){  //将html数据输出到文件
		String str= Data.replaceAll("\\r|\\n","</p><p>"); 
		System.out.println(str);
		//System.out.println(htmlData);
		FileOutputStream f=null;
		PrintStream ps=null;
		try{
			f = new FileOutputStream(path+sp+"HTMLData.html");
			ps = new PrintStream(f);
		}
		catch(Exception e){
			e.getMessage();
		}
		String css = "<style type='text/css'>\nhtml,body{width: 96%;}\n.image{float:left;width:96%;}\ndiv{width:97%;margin 0 auto;}\np{word-wrap:break-word;}</style>";
		ps.println("<html>");
		ps.println("<head>");
		ps.println("<meta charset='utf8'>");
		ps.println("<title>word to html</title>");
		ps.print(css);
		ps.println("</head>");
		ps.println("<body>\n<div>");
		ps.println(str);
		ps.println("</div>\n</body>");
		ps.println("</html>");
	}
	
	
	public void printXml(String xml,String path){ //处理数据并打印成XML 文件
		String[][] xmlData = dataToXML(xml);
		writeToXML(xmlData,path);
	}
	
	private String[][] dataToXML(String str){  //把数据转换为 xml 格式
		//System.out.println(Data);
		str = str.replaceAll("\u0005", "");
		str = str.replaceAll("\u0007", "");
		String[] data = str.split("(\\s\\d\u002e)+");
		int i=1;
		if(data[0].charAt(0)=='1') i = 0;
		String[][] subject = new String[data.length-i][4];
		int index = 0;
		for(;i<data.length;i++){
			int tab,ch,an;
			tab = data[i].indexOf("\u0003");
			ch = data[i].indexOf("A.");
			an = data[i].indexOf("答:");
			
			if(tab!=-1) subject[index][0] = data[i].substring(0,tab); //切出题目
			else if(ch!=-1) subject[index][0] = data[i].substring(0,ch);
			else if(an!=-1) subject[index][0] = data[i].substring(0,an);
			else subject[index][0] = data[i];
			
			if(tab!=-1 && ch!=-1) subject[index][1] = data[i].substring(tab,ch); //切出表格
			else if(tab!=-1 && an!=-1) subject[index][1] = data[i].substring(tab,an);
			else if(tab!=-1) subject[index][1] = data[i].substring(tab,data[i].length());
			
			if(ch!=-1 && an!=-1) subject[index][2] = data[i].substring(ch,an); //切出选项
			else if(ch!=-1) subject[index][2] = data[i].substring(ch,data[i].length());
			
			if(an!=-1) subject[index][3] = data[i].substring(an,data[i].length()); //切出回答
			index++;
		}
		return subject;
	}
	
	private void writeToXML(String[][] xml,String path){ //写入文件
		FileOutputStream f=null;
		PrintStream ps=null;
		try{
			f = new FileOutputStream(path+sp+"XMLData.xml");
			ps = new PrintStream(f);
		}
		catch(Exception e){
			e.getMessage();
		}
		ps.println("<?xml version='1.0' encoding='UTF-8'?>");
		ps.println("<Word>");
		for(int i=0;i<xml.length;i++){
			ps.println("\t<Data>");
				ps.println("\t\t<Subject>");
				ps.println("\t\t\t"+xml[i][0].trim());
				ps.println("\t\t</Subject>");
				
				if(xml[i][1]!=null) ps.println("\t\t\t"+xml[i][1].trim());
				if(xml[i][2]!=null){
					ps.println("\t\t<Choose>");
					ps.println("\t\t\t"+xml[i][2].trim());
					ps.println("\t\t</Choose>");
				}
				if(xml[i][3]!=null){
					ps.println("\t\t<Anser>");
					ps.println("\t\t\t"+xml[i][3].trim());
					ps.println("\t\t</Anser>");
				}
			ps.println("\t</Data>\n\n");
		}
		ps.println("</Word>");
	}
	
}
